
const { ccclass, property } = cc._decorator;

const DAY_START_NUM = 200000
const DAY_INC_NUM = 20000
const HOUR_INC = 500;
const MIN_INC = 100


export interface cityType {
    cityName: string,
    rank?: number,
    university?: number,
    grade?: number,
}


/** 排行榜的类型 */
export interface rankMgrType {
    cityRankInfo: cityType[],
    dau: number,
    succ_user: number
}
@ccclass
export default class RankMgr extends cc.Component {


    // for (var i = 0; i < 100; i++) {
    //     console.log('rand', this.seededRandomRange(0, 10));
    // }

    cityRankData: cityType[] = [{
        "cityName": "河南"
    }, {
        "cityName": "山东"
    }, {
        "cityName": "江苏"
    }, {
        "cityName": "河北"
    }, {
        "cityName": "安徽"
    }, {
        "cityName": "广东"
    }, {
        "cityName": "四川"
    }, {
        "cityName": "山西"
    }, {
        "cityName": "浙江"
    }, {
        "cityName": "湖北"
    }, {
        "cityName": "陕西",
    }, {
        "cityName": "湖南",
    }, {
        "cityName": "辽宁",
    }, {
        "cityName": "福建",
    }, {
        "cityName": "云南",
    }, {
        "cityName": "贵州",
    }, {
        "cityName": "新疆",
    }, {
        "cityName": "北京",
    }, {
        "cityName": "内蒙古",
    }, {
        "cityName": "江西",
    }, {
        "cityName": "甘肃",
    }, {
        "cityName": "黑龙江",
    }, {
        "cityName": "天津",
    }, {
        "cityName": "重庆",
    }, {
        "cityName": "吉林",
    }, {
        "cityName": "广西",
    }, {
        "cityName": "宁夏",
    }, {
        "cityName": "香港",
    }, {
        "cityName": "上海",
    }, {
        "cityName": "海南",
    }, {
        "cityName": "台湾",
    }, {
        "cityName": "西藏",
    }, {
        "cityName": "澳门",
    }, {
        "cityName": "青海",
    }];

    pers = [
        "0.084",
        "0.066",
        "0.059",
        "0.058",
        "0.050",
        "0.050",
        "0.045",
        "0.039",
        "0.036",
        "0.034",
        "0.034",
        "0.032",
        "0.030",
        "0.030",
        "0.029",
        "0.028",
        "0.026",
        "0.025",
        "0.023",
        "0.023",
        "0.020",
        "0.020",
        "0.019",
        "0.018",
        "0.017",
        "0.016",
        "0.013",
        "0.011",
        "0.010",
        "0.010",
        "0.010",
        "0.009",
        "0.009",
        "0.009",
        "0.008"
    ];

    seed: number = null;



    onLoad() { }

    start() {

    }

    getRankInfo() {
        var new_date = new Date();
        let hours = new_date.getHours()
        let min = new_date.getMinutes()
        let sec = new_date.getSeconds()

        var old_date = new Date("2023/11/1 00:00:00");
        var difftime = (Number(new_date) - Number(old_date)) / 1000;
        var days = parseInt(String(difftime / 86400));

        if (days < 0) {
            days = 0
        }
        let dau = DAY_START_NUM + DAY_INC_NUM * days;

        dau += (DAY_INC_NUM / 24 * hours);
        dau += (DAY_INC_NUM / 24 / 60 * min);
        // tnum += (tnum / 3600 * sec);

        dau = parseInt(String(dau))

        this.seed = days + 10000;

        let succ_per = this.seededRandomRange(700, 1200)

        let succ_user = parseInt(String(dau * succ_per / 1000000))
        console.log('tnum == succ_num ', dau, succ_user)

        let city1 = this.cityRankData.slice(0, 10)
        let city2 = this.cityRankData.slice(10, 20)
        let city3 = this.cityRankData.slice(20, 30)
        let city4 = this.cityRankData.slice(30, 36)

        // cc.log('city1', city1, city2, city3, city4, city5, city6, city7)

        // this.seed = days + 1000;
        this.shuffleArray(city1)
        this.shuffleArray(city2)
        this.shuffleArray(city3)
        this.shuffleArray(city4)

        let cityRankInfo = city1.concat(city2).concat(city3).concat(city4)

        let realNums = 0
        for (let index = 0; index < cityRankInfo.length; index++) {
            let element = cityRankInfo[index];
            element.rank = index + 1;
            let score = Math.floor(succ_user * Number(this.pers[index]))
            realNums += score;
            //  大学
            element.university = score * days
            // 小学
            element.grade = Math.floor(element.university * 3.5)
        }


        // cc.log('city1', city1, city2, city3, city4)

        // cc.log('cityRankInfo', cityRankInfo, realNums, trealNums)

        let temp: rankMgrType = {
            cityRankInfo: cityRankInfo,
            dau: dau,
            succ_user: succ_user
        }

        return temp;
    }

    seededRandom(max?, min?) {
        max = max || 1;
        min = min || 0;
        this.seed = (this.seed * 9301 + 49297) % 233280;
        var rnd = this.seed / 233280.0;
        return min + rnd * (max - min);
    };

    seededRandomRange(t, e) {
        var n = Math.max(t, e),
            i = Math.min(t, e);
        return Math.round(this.seededRandom() * (n - i)) + i
    }

    randomRange(t, e) {
        var n = Math.max(t, e),
            i = Math.min(t, e);
        return Math.round(Math.random() * (n - i)) + i
    }

    shuffleArray(array) {
        for (let i = array.length - 1; i > 0; i--) {

            let randomPos = Math.floor(this.seededRandom() * (i + 1));

            [array[i], array[randomPos]] = [array[randomPos], array[i]];
        }
    }
    // update (dt) {}
}
